class Solution {
public:
    int minDays(vector<int>& bloomDay, int m, int k) {
        long long int a = m, b = k;
        if (a * b > bloomDay.size())
        {
            return -1;
        }
        int max_day = 0, min_day = 1000000000;
        for (int i : bloomDay)
        {
            min_day = min(min_day, i);
            max_day = max(max_day, i);
        }
        while (min_day < max_day)
        {
            int day = (min_day + max_day) / 2;
            if (check(bloomDay, m, k, day))
            {
                min_day = day + 1;
            }
            else
            {
                max_day = day;
            }
        }
        return max_day;
    }
    int check(vector<int>& bloomDay, int m, int k, int day)
    {
        int count = 0;
        for (int i : bloomDay)
        {
            if (i <= day)
            {
                count++;
            }
            else
            {
                count = 0;
            }
            if (count == k)
            {
                m--;
                count = 0;
                if (m == 0)
                {
                    break;
                }
            }
        }
        return m > 0 ? 1 : 0;
    }
};